package com.hans.myscanner.test;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.graphics.pdf.PdfRenderer;
import android.util.AttributeSet;
import android.widget.TextView;


import com.hans.myscanner.R;

import org.w3c.dom.Text;

/**
 * Created by Administrator on 2016/6/20.
 */
public class UnderLineEditText extends TextView{
    private static final int LINE_TYPE_STRAIGHT_LINE=1;//直线波浪线
    private static final int LINE_TYPE_WAVE_LINE=2;

    private static final  int LINE_STYLE_DASH=1;//实现虚线
    private static final  int LINE_STYLE_SOLID=2;

    //初始值
    private Paint linePaint;//画图
    private int underLineColor= Color.GRAY;
    private int underLineHeight=1;
    private int lineHeightOffset=0;
    private int lineType=LINE_TYPE_STRAIGHT_LINE;//默认直线实线
    private int lineStyle=LINE_STYLE_SOLID;

    private float waveLineStartX;
    private float waveLineRadius=10f;
    private float waveSpeed=2f;
    private float delta;

    public UnderLineEditText(Context context) {
        this(context,null);
    }

    public UnderLineEditText(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UnderLineEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        lineHeightOffset= (int) (0.19753087f*getLineHeight()+0.5f);

        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.UnderLineEditText);
        underLineColor=ta.getColor(R.styleable.UnderLineEditText_underLineColor,underLineColor);
        underLineHeight=ta.getDimensionPixelSize(R.styleable.UnderLineEditText_underLineHeight,underLineHeight);
        lineHeightOffset=ta.getDimensionPixelSize(R.styleable.UnderLineEditText_lineHeightOffset,lineHeightOffset);
        lineType=ta.getInt(R.styleable.UnderLineEditText_lineType,lineType);
        lineStyle=ta.getInt(R.styleable.UnderLineEditText_lineStyle,lineStyle);
        waveLineRadius=ta.getDimension(R.styleable.UnderLineEditText_waveLineRadius,waveLineRadius);
        waveSpeed=ta.getFloat(R.styleable.UnderLineEditText_waveSpeed,waveSpeed);
        ta.recycle();

        delta= (float) (Math.sqrt(2)/2*waveLineRadius);
        waveLineStartX=-4*delta;//一个周期


        linePaint=new Paint();
        linePaint.setStrokeWidth(underLineHeight);
        linePaint.setColor(underLineColor);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setAntiAlias(true);//用来防止边缘的锯齿//        linePaint.setFilterBitmap(true);

        if (lineStyle==LINE_STYLE_DASH){//虚线
            //
            PathEffect pathEffect = new DashPathEffect(new float[]{5,5,5,5},1);
            linePaint.setPathEffect(pathEffect);
        }
        new WaveMoveThread().start();


    }

    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        int lineCount=getLineCount();
        int height=getHeight();
        int lineHeight=getLineHeight();
        lineCount=Math.max(lineCount,1+height/lineHeight);
        int lineY=getCompoundPaddingTop()+lineHeightOffset;

        //clipRect切割选取的 区域
        canvas.clipRect(getCompoundPaddingLeft(),getCompoundPaddingTop(),getWidth()-getCompoundPaddingRight(),getHeight()-getCompoundPaddingBottom());


        for (int i=0;i<=lineCount;i++){
            lineY += lineHeight;
            if (lineType==LINE_TYPE_STRAIGHT_LINE) {
            Path path = new Path();
            path.moveTo(getCompoundPaddingLeft(), lineY);
            path.lineTo(getWidth() - getCompoundPaddingRight(), lineY);
            canvas.drawPath(path, this.linePaint);
            }else if (lineType==LINE_TYPE_WAVE_LINE){
                drawWaveLine(canvas,waveLineStartX,lineY,getWidth()-getCompoundPaddingRight(),waveLineRadius,linePaint);
            }
           // canvas.save();
        }

    }


    private void drawWaveLine(Canvas canvas,float startX,float startY,float endX,float radius,Paint paint) {
        float x1 = Math.min(startX, endX);
        float x2 = Math.max(startX, endX);
        int n= (int) ((x2-x1)/delta/2+0.99f)+1;
//        float delta = (float) (Math.sqrt(2) / 2 * radius);
//        int n = (int) ((x2 - x1) / delta / 2 + 0.5f);
        float curX, curY, curStartAngle;
        for (int i = 0; i < n; i++) {
            curX = x1 + (2 * i + 1) * delta;
            curY = startY + (i % 2 == 0 ? delta : -delta);
            curStartAngle = i % 2 == 0 ? 225f : 45f;
            drawArc(canvas, curX, curY, radius, curStartAngle, 90f, paint);
        }
    }

    private void drawArc(Canvas canvas,float originX,float originY,float radius,float startAngle,float sweepAngle,Paint paint){
        RectF rectF = new RectF(originX - radius,originY - radius,originX + radius,originY + radius);
        canvas.drawArc(rectF,startAngle,sweepAngle,false,paint);

    }

    private class WaveMoveThread extends Thread{
        private float periodWidth;
        private float waveDistance;
        private float startX;
        private int[] colors={Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.GRAY,Color.BLACK};
        private int colorIndex;

        @Override
        public synchronized void start() {
            super.start();
            periodWidth= (float) (waveLineRadius*2*Math.sqrt(2));
            waveDistance=0;
            startX=-4*delta;
            colorIndex=0;
        }

        @Override
        public void run() {
            while(lineType==LINE_TYPE_WAVE_LINE){
                try {
                    Thread.sleep(100);
                    if (waveDistance+waveSpeed>=periodWidth) waveDistance=0;
                    else waveDistance+=waveSpeed;
                    waveLineStartX=waveDistance+startX;
                    linePaint.setColor(colors[colorIndex++%colors.length]);
                    postInvalidate();//子线程中刷新界面
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }





}




